<?php
    class Image extends Doggy_Flirt_FrontEnd_Controller {

        function Image() {
            parent::Doggy_Flirt_FrontEnd_Controller();
            $this->load->model('image/Image_m');
            $this->load->helper('date');
        }
        function upload_image(){
            is_user();
            if($this->input->post('submit_upload_image')){
                $image_path = './upload_photo';
                $thumb_path = $image_path . '/thumbs';
                $config = array(
                'allowed_types' => "jpg|jpeg|gif|png",
                'upload_path' => $image_path,
                'max_size' => 10000,
                'encrypt_name' =>TRUE
                );
                $this->load->library('upload', $config);

                if ($this->upload->do_upload()) {
                    $upload_info = $this->upload->data();
                    $config1 = array(
                    'source_image' => $upload_info['full_path'], //get original image
                    'new_image' => $thumb_path, //save as new image //need to create thumbs first
                    'maintain_ratio' => true,
                    'width' => 300,
                    'height' => 200
                    );
                    $this->load->library('image_lib', $config1); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $this->_data['image_name'] = $upload_info['file_name'];
                    $this->display_view('image/upload_photo_success');
                } else{
                    echo $this->upload->display_errors();
                }

                $data = array(
                'thumbs'    => $upload_info['file_name'],
                'image'    => $upload_info['file_name'],
                'title'    =>$this->input->post('image_title'),
                'description' =>$this->input->post('image_description'),
                'account_id' =>$this->_data['account_id'],
                'upload_time' =>time(),
                'album_id'=>$this->input->post('album'),
                );
                $album_id = $this->input->post('list_album');
                $this->Image_m->insert_image_album($data);
            }else{
                $user = $this->_data['account_id'] ;
                $this->display_view('home/index');
            }

        }

        function share(){
            is_user();
            if($this->input->post('btn_share_image')){
                $posted_time = now();
                $content = $this->input->post('image_content');
                $posted_by = $this->_data['account_id']; 
                $image_name = $this->input->post('image_name');
                $image_id_max = $this->Image_m->get_max_image_id($this->input->post("album_id"));
                foreach($image_id_max as $rows){
                    $image_name_result = $this->Image_m->get_image_name($rows['max_id']);
                }
                foreach($image_name_result as $row_image_name){
                    $image_name = $row_image_name['image'];   
                }

                $data = array( 
                'content'     => $content,
                'image'       => $image_name,
                'posted_time' => $posted_time,
                'posted_by'   => $posted_by,

                );

                $this->Image_m->insert_image_comment($data);
                redirect('home/show_wall_message'); 
            }else{
                redirect('home/show_wall_message');
            }
        }
        function share_picture($id){
            is_user();
            if($this->input->post('share')){
                $posted_by = $this->_data['account_id']; 
                $query=$this->Image_m->get_image_by_id($id);
                if($query->num_rows()>0)
                {
                    $row=$query->row();
                    $data=array(
                    'image'=>$row->thumbs,
                    'content'=>$this->input->post('image_content'),
                    'posted_time'=>now(),
                    'posted_by'=>$posted_by,
                    'album_id'=>$this->input->post('album'),
                    );
                    $this->Image_m->insert_image_comment($data);
                    redirect('home/show_wall_message'); 
                }
            }
            else{
                $this->display_view('image/upload_photo_success');
            }
        }
        function get_image_by_id($id){
            $query=$this->Image_m->get_image_by_id($id);
            $this->_data['image_detail']=$query;
            $this->display_view('image/add_compte');
        }
        function get_image_shared(){
            is_user();
            $account_id = $this->_data['account_id'];
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 4;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/image/get_image_shared/' . '/' . $per_page . '/';
            $config['per_page'] = $per_page;
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Image_m->count_image_shared($account_id);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['image_galery'] = $this->Image_m->get_image_shared($account_id,$per_page,$off_set);
            $this->_data['total']=$this->Image_m->count_image_shared($account_id);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->display_view('image/my_shared_galary');

        }

        function my_galary(){
           $account_id = $this->_data['account_id'];
           $this->get_all_galary($account_id);
        }

        function get_image_by_album_title(){
            is_user();
            $account_id = $this->_data['account_id'];
            if($this->input->post('submit')){
                $title=$this->input->post('album');
                if($title!= NULL && $title!=''){
                    $this->_data['title_album']=$title;
                    $per_page = $this->uri->segment(3);
                    if ($per_page == "") {
                        $per_page = 24;
                    }
                    $off_set = $this->uri->segment(4);
                    if ($off_set == "") {
                        $off_set = 0;
                    }
                    $config['base_url'] = base_url() . 'index.php/image/get_image_by_album_title/' .$title. '/' . $per_page . '/';
                    $config['per_page'] = '24';
                    $config['uri_segment'] = 5;
                    $config['total_rows'] = $this->Image_m->count_get_image_by_title($title,$account_id);
                    if ($off_set > 0 && $off_set == $config['total_rows']) {
                        $off_set = $off_set - $per_page;
                    }

                    $this->_data['image_galery'] = $this->Image_m->get_image_by_album_title($title,$account_id,$config['per_page'], $off_set);
                    $this->pagination->initialize($config);
                    $this->_data['pagination'] = $this->pagination->create_links();
                    $this->_data['total'] = $this->Image_m->count_get_image_by_title($title,$account_id);
                    $this->_data['per_page'] = $per_page;
                    $this->_data['off_set'] = $off_set;
                    $this->display_view('image/my_galary'); 
                }
                else{
                    $this->get_all_galary($account_id);
                }
            }else{
                $this->get_all_galary($account_id);
            }
        }


        function get_image_private(){
            is_user();
            $account_id = $this->_data['account_id'];
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 5;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/image/get_image_private' . '/' . $per_page . '/';
            $config['per_page'] = $per_page;
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Image_m->count_image_private($account_id);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['image_galery'] = $this->Image_m->get_image_private($account_id,$per_page,$off_set);
            $this->_data['total']=$this->Image_m->count_image_private($account_id);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->display_view('image/my_private_galary');
        }
        function set_private($id)
        {
            $this->Image_m->delete_shared_picture($id);
            redirect('image/get_image_shared');
        }
        public function delete_private_images($id = '')
        {
            // Delete one
            $ids = ($id) ? array($id) : $this->input->post('action_to');

            // Go through the array of ids to delete
            if ( ! empty($ids))
            {
                foreach ($ids as $id)
                {
                    //Goi ham xoa anh private, co xoa file
                    $query=$this->Image_m->get_private_file_name($id);
                    if($query->num_rows()>0){
                        $file_name=$query->row()->image;
                        $image_path='./upload_photo/'.$file_name;
                        $thumb_path='./upload_photo/thumbs/'.$file_name;
                        if($this->Image_m->delete_private_images($id)>0){
                            delete_image($image_path,$thumb_path);
                        }

                    }

                }

            }
            //Redirect
            redirect('image/get_image_private');
        }

        public function delete_shared_images($id = '')
        {

            // Delete one
            $ids = ($id) ? array($id) : $this->input->post('action_to');

            // Go through the array of ids to delete
            if ( ! empty($ids))
            {
                foreach ($ids as $id)
                {
                    //Goi ham xoa anh private, co xoa file
                    $query=$this->Image_m->get_shared_file_name($id);
                    if($query->num_rows()>0){
                        $file_name=$query->row()->image;
                        $image_path='./upload_photo/'.$file_name;
                        $thumb_path='./upload_photo/thumbs/'.$file_name;
                        if($this->Image_m->delete_shared_picture($id)>0){
                            delete_image($image_path,$thumb_path);
                        }

                    }

                }

            }
            //Redirect
            redirect('image/get_image_shared');
        }
        function add_album(){
            is_user();
            if($this->input->post('add')){
                $config = array(
                array(
                'field' => 'title',
                'label' => 'Title',
                'rules' => 'required'
                ),

                );
                $this->form_validation->set_rules($config);
                $this->_data['imageName'] = $this->input->post('imageName');
                if($this->form_validation->run()==TRUE){
                    $data = array(
                    'title'    =>$this->input->post('title'),
                    'is_public' =>1,
                    'account_id' =>$this->_data['account_id'],
                    );
                    $this->Image_m->add_album($data);
                    redirect('image/my_galary');
                }
                else{
                    $this->display_view('image/my_galary');
                }
            }
            else{
                $this->display_view('image/my_galary');
            }
        }
        function get_all_galary($id) {
            $per_page = $this->uri->segment(4) != '' ? $this->uri->segment(4) : 9;
            $off_set = $this->uri->segment(5) != '' ? $this->uri->segment(5) : 0;
            $config['base_url'] = base_url() . 'index.php/image/my_galary/' . $id . '/' . $per_page . '/';
            $config['per_page'] = $per_page;
            $config['uri_segment'] = 5;
            $config['total_rows'] = $this->Image_m->count_image_shared($id);

            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['image_galery'] = $this->Image_m->get_image_shared($id, $per_page, $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->display_view('image/my_galary');
        }

    }
?>